package com.woniu.controller;

import com.woniu.common.core.domain.AjaxResult;
import com.woniu.common.enums.ExceptionCode;
import com.woniu.common.exception.CustomException;
import com.woniu.dto.RegisterDto;
import com.woniu.entity.User;
import com.woniu.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 杨光 微信号:tolryg
 * @time 15:52
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;




    //生成短信验证码
    @RequestMapping("web/generateCellphoneCode")
    public AjaxResult generateCellphoneCode(String cellphone,HttpServletRequest request) {
        long count = stringRedisTemplate.opsForValue().increment("user_register_cellphoneCode_limit_" + cellphone);
        if (count > 3) {
            stringRedisTemplate.opsForValue().set("user_register_cellphoneCode_limit_" + cellphone, "0", 24, TimeUnit.HOURS);
            throw new CustomException(ExceptionCode.CELL_PHONE_VERIFY_CODE_LIMIT);
        }

        String remoteAddr = request.getRemoteAddr();
        System.out.println("remoteAddr = " + remoteAddr);

        //1.生成手机验证码
        long time = System.currentTimeMillis();
        String code = String.valueOf(time).substring(9);
        //2.把验证码放入redis中，并且设置1分钟超时时间
        stringRedisTemplate.opsForValue().set("user_register_cellphoneCode_" + cellphone, code, 60, TimeUnit.SECONDS);
        //3.把手机号和验证码发给rabbitmq
        rabbitTemplate.convertAndSend("woniu84_register_code_exchange",
                "woniu84_register_code_routigkey",cellphone+"_"+code);
        System.out.println("code = " + code);
        return AjaxResult.success();
    }


    @RequestMapping("web/register")
    public AjaxResult register(@RequestBody RegisterDto registerDto){
        userService.register(registerDto);
        return AjaxResult.success();
    }

    @RequestMapping("feign/findByUserAccountAndPassword")
    public User findByUserAccountAndPassword(
            @RequestParam("userAccount") String userAccount,
            @RequestParam("password") String password

    ) {
        return userService.findByUserAccountAndPassword(userAccount,password);
    }


    @RequestMapping("getInfo")
    public AjaxResult getInfo(HttpServletRequest request) {
        AjaxResult ajax = AjaxResult.success();

//        String userIdStr = request.getHeader("userid");//这个请求头是网关添加的
//        if (userIdStr != null) {
//            Integer userId = Integer.valueOf(userIdStr);
        //由于目前只有一个admin用户，这里就不查询数据库了，直接返回超级用户的权限
        Set<String> roles = new HashSet<>();
        roles.add("admin");
        Set<String> perms = new HashSet<String>();
        perms.add("*:*:*");
        User user = new User();
        user.setUserName("admin");
        user.setUserAccount("admin");
        user.setUserId(1);
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", perms);
//        }
        return ajax;
    }
}
